package com.jdwx.opengl.kotlin

import java.lang.Integer.parseInt
import java.util.*


fun main(){

//    var a = "1"
//    var b = "1"
//    if( a.equals( b ) ){
//        println( "true" )
//    }
//    class A{
//
//    }
//    var aa = A()
//    var bb = aa
//
//    if( aa.equals( bb ) ){
//        println( "true" )
//    }
//
//    data class Point(val x: Int, val y: Int)
//
//    operator fun Point.unaryMinus() = Point(-x, -y)
//
//    val point = Point(10, 20)
//    println(point.x)  // 输出“Point(x=-10, y=-20)”
//
//    var a2 = 10
//
//    println( a2.inc() )
//    println( a2.dec() )
//
//    class Cat(var name:String,var gender:String){
////使用operator关键字定义了两个函数
////需要注意的是，函数的名称一定是 component+数字()
////这时，如果调用了component1，那么得到的就是name
////接下来去看 主方法类，Main类
//        operator fun component1() =name
//        operator fun component2() =gender
//    }
//    val cat =Cat("Tom", "boy")
//    //val (name,gender) =cat
//
//    print( cat.name )
//    print( cat.component1() )
//
//
//    class Person(var name: String)
//
//    operator fun Person.plus(p: Person): Person {
//        this.name += p.name
//        print("==")
//        return this
//    }
//
//
//    println( (Person("1") + Person("2") + Person("2")).name )
//



    var a = 1
    var b = -2
    var c = true
    var d = false

    // 操作符实现
    println("+a = ${+a}\t -a = ${-a}\t !c = ${!c}")
    println("------------------------------------------")
    println("+b = ${+b}\t -b = ${-b}\t !d = ${!d}")
    println("------------------------------------------")
    // 操作符重载实现
    println("+a = ${a.unaryPlus()}\t -a = ${a.unaryMinus()}\t !c = ${c.not()}")
    println("------------------------------------------")
    println("+b = ${b.unaryPlus()}\t -b = ${b.unaryMinus()}\t !d = ${d.not()}")


    var bc: String? = "abc"
    bc = null // ok
    print(bc)
    var l = 0
    if( bc !=null  ) {
        l = bc.length
    }else{
        l = -1
    }
    l = if (bc != null) bc.length else -1


    println( "l=" + l )

    val aa = "Kotlin"
    val bb: String? = null
    println(bb?.length)
    println(aa?.length) // 无需安全调用


    val listWithNulls: List<String?> = listOf("Kotlin", null)
    for (item in listWithNulls) {
        item?.let { println(it) } // 输出 Kotlin 并忽略 null
    }

    var ss = "r"

    val aInt: Int? = ss as? Int


    println( "aInt="+aInt )

    val nullableList: List<Int?> = listOf(1, 2, null, 4)
    val intList: List<Int> = nullableList.filterNotNull()

    print( intList )

    bb?.let {
        print("不为null")
    }
    ss?.let {
        print("不为null")
    }



    try {
        // 一些代码
        //throw Exception("Hi There!")
    }
    catch (e: Exception) {
        // 处理程序
        e.printStackTrace()

    }
    finally {
        // 可选的 finally 块
        println("finally")
    }
    println("=================")
    var input = "ss"
    val aaa: Int? = try { parseInt(input) } catch (e: NumberFormatException) { null }

    println( "aaa="+aaa )

    fun fail(message: String): Nothing {
        throw IllegalArgumentException(message)
    }

    //val s = bb ?:  fail("Name required")
    //println(s)     // 在此已知“s”已初始化

    var ab = null

    var sss = ab ?: 10

    print( sss )

    //Java原生的ArrayList
    val javaList = ArrayList<Int>()
    javaList.add(10)

    for (i in javaList){
        println( i )
    }

    val x = null           // “x”具有类型 `Nothing?`
    val ll = listOf(null)   // “l”具有类型 `List<Nothing?>

    println( ll )
}